package cn.imvc.util;

import java.util.Collection;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.PropertyFilter;

/**
 * JSON 工具类
 * 
 * @author lijibo
 *
 */
public class JSONUtils {
	/**
	 * 过滤器：去除为空和为集合的属性
	 */
	public final static PropertyFilter WITHOOUT_COLLENTION_PROPERTY_FILTER = new PropertyFilter() {
		@Override
		public boolean apply(Object object, String name, Object value) {
			if (value == null || value instanceof Collection) {
				return !object.getClass().getName().startsWith("cn.imvc.app.entities");
			}
			return true;
		}
	};

	/**
	 * 转换为json串，去除为空和为集合类型的属性
	 * @param o
	 * @return
	 */
	public static String toJSONStringWithoutCollectionProperty(Object o) {
		return JSON.toJSONString(o, WITHOOUT_COLLENTION_PROPERTY_FILTER);
	}
}
